<!--
 * @Author: coman
 * @LastEditors: coman
 * @Date: 2020-06-16 17:04:04
 * @motto: No pain,No gain
 * @LastEditTime: 2020-06-16 21:15:59
--> 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!--有一个从小到大排好序的数组。现输入一个数，要求按原来的规律将它插入数组中。  -->
    <input type="text" id="num1">
    <button id="btn">插入数据</button>
    <br>
    <span id="res">1,2,6</span>
    <script>
        btn.onclick = function(){
            var num='';
            var index=0;
            var arr = res.innerHTML.split(",");
             num =  Number(num1.value);
            index = arr.length;
            if(num>=arr[index-1]){
                   arr[index]=num;
                }else{
                    for(var i=0;i<arr.length;i++){
                        if(num<arr[i])  {    
                            for(var j=arr.length-1;j>=i;j--){
                                arr[j+1]=arr[j];
                                console.log(j);
                                
                                     }
                                arr[i]=num;
                                break;}
                            
                                    }
                }
            console.log(arr);
            var txt =arr.join(",")
          console.log(txt);
    
            res.innerHTML=txt;
        }
    </script>
</body>
</html>